﻿using liaowa2.Entity.DTO;
using liaowa2.Interface.Manager;
using liaowa2.Utils;
using liaowa2.Utils.Cookie;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace liaowa2.Web.Controllers
{
    public class LoginController : Controller
    {

        private IManagerUserInterface _IManagerUserService;

        public LoginController(IManagerUserInterface ManagerUserService)
        {
            _IManagerUserService = ManagerUserService;
        }


        // GET: Login
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Login(string UserName, string password)
        {

            var result = _IManagerUserService.ManagerLogin(UserName, EncodeSecurity.EncryptString(password));
            if (!result.State)
            {
                return Json(result.Message);
            }

            if (result.Single != null)
            {
                CookieHelper.WriteCookieByDay(ParameterConfig.CookieName, EncodeSecurity.EncryptString(JsonConvert.SerializeObject(result.Single)), 1);
                ViewBag.UserName = (result.Single as ManagerUser).user_name;
            }

            return Json(1);

        }


        public ActionResult OutLogin()
        {
            CookieHelper.ClearCookie(ParameterConfig.CookieName);
            Response.Redirect("/Login/Index");
            return View();
        }

        //public ActionResult UpPwd(FormCollection collection)
        //{
        //    string oldPwd = collection["txtOldPassword"];
        //    string newPwd = collection["txtPassword"];
        //    string conPwd = collection["txtPassword1"];
        //    if (!string.IsNullOrEmpty(oldPwd) && !string.IsNullOrEmpty(newPwd) && !string.IsNullOrEmpty(conPwd))
        //    {
        //        if (newPwd == conPwd)
        //        {
        //            try
        //            {
        //                var result = _IManagerUserService.ManagerChangePwd(GetAdmin().UserName, EncodeSecurity.EncryptString(oldPwd), newPwd);
        //                if (result > 0)
        //                {
        //                    AlertMessage("原密码错误！");
        //                    return null;
        //                }
        //                else
        //                {
        //                    CookieHelper.ClearCookie(ParameterConfig.CookieName);
        //                    return Content("<script>alert('密码修改成功,请重新登录');window.parent.location.href='/login/Index'</script>");
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                AlertMessage(ex.Message);
        //                return null;
        //            }
        //        }
        //        else
        //        {
        //            AlertMessage("两次输入的密码不正确");
        //            return null;
        //        }
        //    }
        //    else
        //    {
        //        AlertMessage("参数不正确");
        //        return null;
        //    }
        //}


    }
}